package com.example.geta;

import cn.hutool.core.date.DateUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;

public class CaoLiu {
    String  host = "https://cl.159x.xyz/";

    @Test
    public void C1024() {
        getPicDownUrlFromAllPicPage(1,100, "露出");
    }

    public void getPicDownUrlFromAllPicPage( int startPage,int endPage, String key) {
        StringBuilder sb = new StringBuilder();
        for (int i = startPage; i < endPage; i++) {
            System.out.println("正在读取第 " + i + " 页");
            //保存图片的下载地址
            try {
                Document doc = Jsoup.connect(host+"thread0806.php?fid=16&search=&page=" + i ).get();
                for (Element element : doc.select(".tal h3 a")) {
                    String html = element.html();
                    if (html.contains(key)) {
                        System.out.println("==>读取" + element);
                        String href = element.attr("href");
                        // 详情页
                        sb.append("<div>")
                                .append("<a href='").append(host).append(href).append("' >").append(html)
                                .append("</a>")
                                .append("</div>");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String fileName = "1024"+DateUtil.format(new Date(), "yyyyMMddHHmmss")  + ".html";
        try {
            Files.write(Paths.get("C:\\Users\\Administrator\\Desktop\\a", fileName), sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
